import fetch from 'common/src/utils/fetch';
import store from 'common/src/store/index';
import UTILS from "common/src/utils/utils";
/*
 * 查询数据字典
 * @param data
 */
  export function queryDic(data) {
    let authId = localStorage.getItem('activeAuthId');
    if (authId) {
        data['authId'] = authId;
    }
    return fetch({
        url: '/api/commonservice/dictionary/query',
        method: 'post',
        headers: { 'Content-Type': 'application/json' },
        data
    });
}

export function queryZYFX(dat) {
  let str = String(dat)
  return fetch({
    url: '/api/commonservice/directionMainten/findDirectionMaintenByCode/' + str,
    method: 'GET'
  });
}

/**
 * 培养类别下拉排序
 */
export function sortTrainingCategories(data) {
    return fetch({
        url: '/api/commonservice/dictionary/getDictionaryForList',
        method: 'POST',
        data
    });
}
/*
 * 查询数据字典(本研互选)
 * @param data
 */
export function queryMutualDic(data) {
    let authId = localStorage.getItem('activeAuthId');
    if (authId) {
        data['authId'] = authId;
    }
    let projectId = store.getters.getProjectId();
    if(projectId == 1) {
        data['projectId'] = 2;
        return fetch({
            url: '/api/commonservice/dictionary/query',
            method: 'post',
            headers: { 'Content-Type': 'application/json' },
            data
        }).then(res => {
            data['projectId'] = 4;
            return fetch({
                url: '/api/commonservice/dictionary/query',
                method: 'post',
                headers: { 'Content-Type': 'application/json' },
                data
            }).then(res2 => {
                if(res2.data) {
                  for(let key in res2.data){
                      res.data[key] = Object.assign(res.data[key], res2.data[key])
                  }
                }
                return res;
            });
        })
    } else if (projectId == 2 || projectId == 4) {
      data['projectId'] = 1;
      return fetch({
          url: '/api/commonservice/dictionary/query',
          method: 'post',
          headers: { 'Content-Type': 'application/json' },
          data
      })
    } else {
      // 预留
    }
}
//查询数据字典（本研互选：名单查询）
export function queryMutualDicListName(data) {
    let authId = localStorage.getItem('activeAuthId');
    if (authId) {
        data['authId'] = authId;
    }
    let projectId = data.projectId;
    if(projectId == 1) {
        data['projectId'] = 1;
        return fetch({
            url: '/api/commonservice/dictionary/query',
            method: 'post',
            headers: { 'Content-Type': 'application/json' },
            data
        })

    } else {
       data['projectId'] = 2;
        return fetch({
            url: '/api/commonservice/dictionary/query',
            method: 'post',
            headers: { 'Content-Type': 'application/json' },
            data
        }).then(res => {
            data['projectId'] = 4;
            return fetch({
                url: '/api/commonservice/dictionary/query',
                method: 'post',
                headers: { 'Content-Type': 'application/json' },
                data
            }).then(res2 => {
                if(res2.data) {
                  for(let key in res2.data){
                      res.data[key] = Object.assign(res.data[key], res2.data[key])
                  }
                }
                return res;
            });
        })
    }
}
export function queryMutualNatureData(data){//名單查詢課程性質特殊處理
    data['projectId'] = 1;
    return fetch({
        url: '/api/commonservice/dictionary/query',
        method: 'post',
        headers: { 'Content-Type': 'application/json' },
        data
    })

}
/*
 * 根据学院查询对应的专业
 * @param collage [string|array]
 */
export function queryMajors(collage) {
    let data = {
        lang: store.getters.language || 'cn',
        type: 'allChild',
        keys: ['G_ZY'],
        filter: {
            prefix: typeof collage === 'string' ? [collage] : collage,
            specilaCode: 'XY2ZY'
        }
    };

    return queryDic(data);
}

/* 以下是数据字典CRUD部分 */

/**
 * 添加数据字典
 */
export function insertDict(data) {
    return fetch({
        url: '/api/commonservice/dictionary/insertDict',
        method: 'POST',
        data
    });
}
/**
 * 修改数据字典
 */
export function updateDict(data) {
    return fetch({
        url: '/api/commonservice/dictionary/updateDict',
        method: 'PUT',
        data
    });
}
/**
 * 删除数据字典  支持批量删除
 */
export function deleteDictById(data) {
    return fetch({
        url: '/api/commonservice/dictionary/dict',
        method: 'DELETE',
        data
    });
}

/**
 * 批量修改数据字典表类型
 *
 */
export function updateDictType(data) {
    return fetch({
        url: '/api/commonservice/dictionary/updateDictType',
        method: 'PUT',
        data
    });
}

/**
 * 根据id查询字典表信息
 */
export function getDictById(id) {
    return fetch({
        url: '/api/commonservice/dictionary/getDictById/' + id,
        method: 'GET'
    });
}

/**
 *  获取字典表列表，根据type分组
 */
export function getDictionarys() {
    return fetch({
        url: '/api/commonservice/dictionary/getDictionarys/type',
        method: 'GET'
    });
}

export function refreshCache() {
    return fetch({
        url: '/api/commonservice/dictionary/refreshCache',
        method: 'GET'
    });
}

/**
 * 查询符合条件的字典表  模糊分页查询
 */
export function getDictList(pageNum, pageSize, data) {
    return fetch({
        url:
            '/api/commonservice/dictionary/getDictList/page/' +
            pageNum +
            '/' +
            pageSize,
        method: 'POST',
        data
    });
}

export function findDictTypeByRelateType(type) {
    return fetch({
        url:
            '/api/commonservice/dictionary/findDictTypeByRelateType?relateType=' +
            type,
        method: 'GET'
    });
}

export function exportDictionaryList(types) {
    window.open(
        '/api/commonservice/dictionary/exportDictionaryList?types=' + types
    );
}

export function findTrainingLevelByProjId(id) {
    return fetch({
        url:
            '/api/commonservice/dictionary/findTrainingLevelByProjId' +
            (id ? '?projId=' + id : ''),
        method: 'GET'
    });
}

export function findDictByCondition(data = {}) {
    return fetch({
        url: '/api/commonservice/dictionary/queryDictByCondition',
        method: 'POST',
        data
    });
}

/**
 * 教室页面查询全量部门
 */
export function findDeptForClassroom(data) {
    return fetch({
        url: '/api/userservice/dept/findDeptForClassroom',
        method: 'POST',
        data
    });
}

/**
 * 培养方案高级导出
 */
export function advanceExport(id,semester) {
    return fetch({
        url:`/api/cultureservice/bclCultureScheme/exportCultureSemesterWord2?id=${id}&semester=${semester}`,
        method: 'get',
        headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, '培养方案-高级导出.pdf', 'application/vnd.ms-pdf;charset=utf-8');
    });
}
